<template>
	<view>
		<view style="width: 90%; margin: 200rpx auto;">
			<view style="margin-bottom: 70rpx; font-size: 60rpx; color: royalblue; text-align: center;">程序猿侠</view>
			<uni-forms ref="form" :modelValue="loginForm" :rules="rules">
				<uni-forms-item name="username">
					<uni-easyinput v-model="loginForm.username" prefixIcon="person" placeholder="请输入登录账号"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput type="password" v-model="loginForm.password" prefixIcon="locked" placeholder="请输入登录密码"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<view>
				<button type="primary" @click="loginBtn">登 录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {Encrypt} from '../../utils/aes.js'
	export default {
		data() {
			return {
				loginForm: {
					username:'chengqiang',
					password:'chengqiang',
					grant_type:'password',
					client_id:'client-app',
					client_secret:'123456'
				},
				rules: {
					username:  {
						rules:[{required: true,errorMessage: '请输入账号'}],
						validateTrigger:'submit',
					},
					password:  {
						rules:[{required: true,errorMessage: '请输入密码'}],
						validateTrigger:'submit',
					}
				}
			}
		},
		onShow() {
			uni.hideTabBar();
		},
		// onHide() {
		// 	uni.showTabBar();
		// },
		methods: {
			/* 登录请求 */
			loginBtn() {
				this.loginForm.password = Encrypt(this.loginForm.password);
				this.$refs.form.validate().then(res=>{
					this.postKeyValueRequest('/authorization/oauth/token',this.loginForm).then(resp=>{
					    if (resp){
							/* 存储当前登录用户 */
							uni.setStorageSync('userInfo',resp.data);
							/* 提示登录成功 */
							uni.showToast({
								title:'登陆成功'
							});
					        /* 登录成功跳转主页 */
							let timer = setTimeout(()=>{
								clearTimeout(timer);
								uni.switchTab({
									url:'/pages/index/index'
								});
							},1000);
					    }
					})
				}).catch(err =>{
				})
			}
		}
	}
</script>

<style scoped>

</style>
